package com.byedu.course.controller;

import com.byedu.course.util.ControllerUtil;
import com.byedu.course.util.UserExportExcelUtil;
import com.byedu.domain.focus.*;
import com.byedu.domain.focus.vo.enumvos.FocusStatusTypeVO;
import com.byedu.domain.focus.vo.enumvos.FocusdepaTypeEnum;
import com.byedu.domain.focus.vo.FocusKpiVO;
import com.byedu.domain.focus.vo.FocusProjectVO;
import com.byedu.domain.focus.vo.FocusUserVO;
import com.byedu.domain.focus.vo.JiduVO;
import com.byedu.focus.*;
import com.byedu.util.CommonUtil;
import com.byedu.util.FileUploadUtil;
import com.byedu.util.yunpian.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Luo on 2017-06-28.
 */
@Controller
@RequestMapping("/clazz")
public class AdminClazzController {

    private static final Logger logger = LoggerFactory.getLogger(AdminClazzController.class);

    @Autowired
    private FocusProduceService focusProduceService;
    @Autowired
    private FocusUserService focusUserService;
    @Autowired
    private FocusKpiService focusKpiService;
    @Autowired
    private FocusProjectService focusProjectService;
    @Autowired
    private FocusLogService focusLogService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String m = request.getParameter("m");
        FocusUserVO adminUser = ControllerUtil.checkuser(request, response);
        ControllerUtil.saveuser(request, focusProjectService, focusUserService, adminUser);

        if(StringUtils.isEmpty(m)){
            m = "clazz";
        }
        request.setAttribute("m", m);
        Calendar a=Calendar.getInstance();
        List<FocusKpiVO> kpiList= null;
        FocusKpiDO focusKpiDO = null;
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_1");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_1");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);

        request.setAttribute("firstsnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_2");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_2");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("secondnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_3");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_3");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("thridnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_4");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_4");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("fourthnum",focusKpiDO.getKipnum());
        return "index";
    }

    @RequestMapping("/prejiazai")
    public String  prejiazai(HttpServletRequest req,HttpServletResponse resp) throws IOException {

        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        String id = req.getParameter("projectid");
        FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(id);
        List<FocusBugetDO> bugetList = focusProjectService.getFocusPreBugetDOList(id);
        req.setAttribute("projectEntity",projectEntity);
        req.setAttribute("bugetList",bugetList);
        return ControllerUtil.toHtml(adminUser,"clazz/addpreyusuan",req,resp);
    }

    @RequestMapping("/addprebuget")
    public String  addprebuget(HttpServletRequest req,HttpServletResponse resp) throws IOException {

        int len = CommonUtil.StringToInt(req.getParameter("inputlen"));
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        String projectid = req.getParameter("projectid");
        System.out.println("projectid"+projectid);
        List<FocusBugetDO> focusBugets = new ArrayList<FocusBugetDO>();
        if(!StringUtil.isNullOrEmpty(projectid)) {
            for(int i=1;i<=len;i++){
                FocusBugetDO focusBugetDO = new FocusBugetDO();

                String cagetary =req.getParameter("cagetary"+i);

                String money =req.getParameter("money"+i);
                String info =req.getParameter("info"+i);
                focusBugetDO.setProjectId(Integer.parseInt(projectid));
                focusBugetDO.setPremoney(money);
                focusBugetDO.setCagetary(cagetary);
                focusBugetDO.setMoney(money);
                focusBugetDO.setInfo(info);
                focusBugets.add(focusBugetDO);
            }
            focusProjectService.updateFocusPreBuget(Integer.parseInt(projectid),focusBugets);
        }


        FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(projectid);
        req.setAttribute("projectEntity",projectEntity);
        return ControllerUtil.toHtml(adminUser,"clazz/addunfinished",req,resp);
    }



    @RequestMapping("/jiazai")
    public String  jiazai(HttpServletRequest req,HttpServletResponse resp) throws IOException {

        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        String id = req.getParameter("projectid");
        FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(id);
        List<FocusBugetDO> bugetList = focusProjectService.getFocusBugetDOList(id);
        req.setAttribute("projectEntity",projectEntity);
       req.setAttribute("bugetList",bugetList);
        return ControllerUtil.toHtml(adminUser,"clazz/addyusuan",req,resp);
    }

    @RequestMapping("/addbuget")
    public String  addbuget(HttpServletRequest req,HttpServletResponse resp) throws IOException {

        int len = CommonUtil.StringToInt(req.getParameter("inputlen"));
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        String projectid = req.getParameter("projectid");
        System.out.println("projectid"+projectid);
        List<FocusBugetDO> focusBugets = new ArrayList<FocusBugetDO>();
        if(!StringUtil.isNullOrEmpty(projectid)) {
            for(int i=1;i<=len;i++){
                    FocusBugetDO focusBugetDO = new FocusBugetDO();

                    String cagetary =req.getParameter("cagetary"+i);
                    String premoney =req.getParameter("premoney"+i);
                    String money =req.getParameter("money"+i);
                    String info =req.getParameter("info"+i);
                    focusBugetDO.setProjectId(Integer.parseInt(projectid));
                    focusBugetDO.setPremoney(premoney);
                    focusBugetDO.setCagetary(cagetary);
                    focusBugetDO.setMoney(money);
                    focusBugetDO.setInfo(info);
                    focusBugets.add(focusBugetDO);
                }
            focusProjectService.updateFocusBuget(Integer.parseInt(projectid),focusBugets);
        }


        FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(projectid);
        req.setAttribute("projectEntity",projectEntity);
        return ControllerUtil.toHtml(adminUser,"clazz/addunfinished",req,resp);
    }

    @RequestMapping("/produce")
    public String  produce(HttpServletRequest req,HttpServletResponse resp) throws IOException {

        String m = req.getParameter("m");
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        List<String> customerList = focusProduceService.getcustomerList();
        List<FocusProduceDO> produceList = null;

        req.setAttribute("customerList", customerList);
        if(m.equals("list")){
            customerList.add("全部");
            String customer = StringUtil.getNullOrEmpty(req.getParameter("customer"));
            String key =  StringUtil.getNullOrEmpty(req.getParameter("key"));
            String name = null;
            if(!StringUtil.isNullOrEmpty(customer)){
                 customer = new String(customer.getBytes("iso8859-1"), "utf-8");
            }
            if(!StringUtil.isNullOrEmpty(key)){
                 name = new String(key.getBytes("iso8859-1"), "utf-8");
            }
            FocusProduceDO focusProduceDO = new FocusProduceDO();
            focusProduceDO.setCustomer(customer);
            focusProduceDO.setProducename(name);
            produceList = focusProduceService.getFocusProduceListbyFocusProduceDO(focusProduceDO);
            req.setAttribute("downexcel", UserExportExcelUtil.produceExcelexpot(produceList,adminUser));
            req.setAttribute("produceList", produceList);
        }
        if(m.equals("add")){
            return ControllerUtil.toHtml(adminUser,"clazz/addproduce",req,resp);
        }

        if(m.equals("edit")){
            String id = req.getParameter("id");
            FocusProduceDO produceEntity = focusProduceService .getFocusProducebyId(id);
            req.setAttribute("produceEntity",produceEntity);
            return ControllerUtil.toHtml(adminUser,"clazz/addproduce",req,resp);
        }
        if(m.equals("check")){
            String id = req.getParameter("id");
            FocusProduceDO produceEntity = focusProduceService .getFocusProducebyId(id);
            req.setAttribute("produceEntity",produceEntity);
            return ControllerUtil.toHtml(adminUser,"clazz/checkproduce",req,resp);
        }
        return "clazz/produceList";
    }

    @ResponseBody
    @RequestMapping("/addproduce")
    public BaReSult addproduce(HttpServletResponse resp, HttpServletRequest req,
                               FocusProduceDO focusProduceDO
    ) throws IOException {
        String m = req.getParameter("m");
        System.out.println(focusProduceDO.toString());
        Integer retInt = 0;
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        if(adminUser.getPermission().getCode() <= FocusdepaTypeEnum.AE.getCode()){
            if(m.equals("doadd")) {

                retInt = focusProduceService.addFocusProduceDO(focusProduceDO);
                if(retInt > 0){
                    focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"添加产品",focusProduceDO.toString()));
                }
            } else {

                retInt = focusProduceService.updateFocusProduceDObyId(focusProduceDO);
                if(retInt > 0){
                    focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"更新产品",focusProduceDO.toString()));
                }
            }
        }else {
            if (m.equals("doedit"))  {

                retInt = focusProduceService.updateFocusProduceDObyId(focusProduceDO);
                if(retInt > 0){
                    focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"更新产品",focusProduceDO.toString()));
                }
            }
        }

        if(retInt > 0){
            return new BaReSult(true);
        }else{
            return new BaReSult("请联系管理员");
        }
    }


    @ResponseBody
    @RequestMapping("/delproduce")
    public BaReSult delproduce(HttpServletResponse resp, HttpServletRequest req,
                            FocusProduceDO focusProduceDO
    ) throws IOException {
//        return new BaReSult("操作失败，请联系管理员");
        Integer retInt = 0;
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        if(adminUser.getPermission().getCode()== FocusdepaTypeEnum.BOSS.getCode()){

            retInt= focusProduceService.delFocusProduceDO(focusProduceDO);
        }else {
            return new BaReSult("权限不够");
        }
        if(retInt>0){
            focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"删除产品",focusProduceDO.toString()));
            return new BaReSult(true);
        }else{
            return new BaReSult("请联系管理员");
        }
    }



    @RequestMapping("/project")
    public String  project(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String m = req.getParameter("m");
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        ControllerUtil.saveuser(req, focusProjectService, focusUserService, adminUser);
        List<FocusProduceDO>  produceList  = focusProduceService.getFocusProduceDOList();

        List<JiduVO> jiduList  = focusKpiService.getjifuList();
        req.setAttribute("jiduList",jiduList);

        List<FocusUserVO> aes  = focusUserService.getFocusUserListbypermission(FocusdepaTypeEnum.AE.getCode());
//        List<FocusCategoryTypeVO> focusCategoryTypeVOList = focusProjectService.getFocusCategoryTypeVOList();
        List<FocusStatusTypeVO> focusStatusTypeVOList = focusProjectService.getFocusStatusTypeVOList();

        req.setAttribute("produceList",produceList);
        req.setAttribute("aes",aes);
//        req.setAttribute("categorys",focusCategoryTypeVOList);
        req.setAttribute("status",focusStatusTypeVOList);

//        req.setAttribute("jiduList",jiduList);

        if(m.equals("list")){
            List<FocusProjectVO> projectList =null;
            String ptid = StringUtil.getNullOrEmpty(req.getParameter("ptid"));
            String jidu = StringUtil.getNullOrEmpty(req.getParameter("jidu"));
            String name = StringUtil.getNullOrEmpty(req.getParameter("key"));
            String aeid = StringUtil.getNullOrEmpty(req.getParameter("aesid"));
            String statuid = StringUtil.getNullOrEmpty(req.getParameter("statuid"));

            if(adminUser.getPermission().getCode() != FocusdepaTypeEnum.AE.getCode()){
                if(!StringUtil.isNullOrEmpty(jidu)) {
                    String[] timeStr= jidu.split("_");

                    projectList =   focusProjectService.getFocusProjectVOListByTimeandprid(null,timeStr,ptid,name,aeid,statuid);

                }else {
                    projectList =   focusProjectService.getFocusProjectVOListByTimeandprid(null,null,ptid,name,aeid,statuid);
                }
            }else{
                if(!StringUtil.isNullOrEmpty(jidu)) {

                    String[] timeStr= jidu.split("_");
                    projectList =   focusProjectService.getFocusProjectVOListByTimeandprid(adminUser.getId(),timeStr,ptid,name,aeid,statuid);

                }else {
                    projectList =   focusProjectService.getFocusProjectVOListByTimeandprid(adminUser.getId(),null,ptid,name,aeid,statuid);
                }
            }
                FocusKpiDO focusKpiDO = focusKpiService.getprojectListSum(projectList);
                req.setAttribute("sum",focusKpiDO);
                req.setAttribute("downexcel", UserExportExcelUtil.projectExcelexpot(projectList, focusKpiDO,adminUser));
                req.setAttribute("projectList",projectList);
        }
        if(m.equals("add")){

            return ControllerUtil.toHtml(adminUser,"clazz/addproject",req,resp);
        }
        if(m.equals("edit")){
            String id = req.getParameter("id");
            FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(id);
            req.setAttribute("projectEntity",projectEntity);
            return ControllerUtil.toHtml(adminUser,"clazz/addproject",req,resp);
        }
        if(m.equals("check")){
            String id = req.getParameter("id");
            FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(id);
            req.setAttribute("projectEntity",projectEntity);
            return ControllerUtil.toHtml(adminUser,"clazz/checkproject",req,resp);
        }
        return "clazz/projectList";
    }

    @ResponseBody
    @RequestMapping("/addproject")
    public BaReSult addproject(HttpServletRequest req,HttpServletResponse resp,FocusProjectDO focusProjectDO ) throws IOException {
        String m = req.getParameter("m");
        System.out.println(focusProjectDO.toString());
        Integer retInt = 0;
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        NumberFormat format = NumberFormat.getNumberInstance() ;
        format.setMaximumFractionDigits(2);

        Double planrate  = (CommonUtil.stringToDouble(focusProjectDO.getPlannum())- CommonUtil.stringToDouble(focusProjectDO.getPlancomoutnum())- CommonUtil.stringToDouble(focusProjectDO.getPlanprioutnum()))/CommonUtil.stringToDouble(focusProjectDO.getPlannum())/1.07;
            focusProjectDO.setPlanrate(format.format(planrate));
        Double realrate  = (CommonUtil.stringToDouble(focusProjectDO.getPonum())- CommonUtil.stringToDouble(focusProjectDO.getRealcomoutnum())- CommonUtil.stringToDouble(focusProjectDO.getRealprioutnum()))/CommonUtil.stringToDouble(focusProjectDO.getPonum())/1.07;
        focusProjectDO.setRealrate(format.format(realrate));

        if(adminUser.getPermission().getCode() <= FocusdepaTypeEnum.AE.getCode()){
            if(m.equals("doadd")) {

                retInt = focusProjectService.addFocusProjectDO(focusProjectDO);
                if(retInt > 0){
                    focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"添加项目",focusProjectDO.toString()));
                }
            } else {

                retInt = focusProjectService.updateFocusProjectDObyId(focusProjectDO);
                if(retInt > 0){
                    focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"更新项目",focusProjectDO.toString()));
                }
            }
        }else {
            if (m.equals("doedit"))  {

                retInt = focusProjectService.updateFocusProjectDObyId(focusProjectDO);
                if(retInt > 0){
                    focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"更新项目",focusProjectDO.toString()));
                }
            }
        }

        if(retInt > 0){
            return new BaReSult(true);
        }else{
            return new BaReSult("请联系管理员");
        }
    }


    @ResponseBody
    @RequestMapping("/delproject")
    public BaReSult delproject(HttpServletResponse resp, HttpServletRequest req,
                               FocusProjectDO focusProjectDO
    ) throws IOException {
//        return new BaReSult("操作失败，请联系管理员");
        Integer retInt = 0;
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        if(adminUser.getPermission().getCode()== FocusdepaTypeEnum.BOSS.getCode()){

            retInt= focusProjectService.delFocusProjectDO(focusProjectDO);
        }else {
            return new BaReSult("权限不够");
        }
        if(retInt>0){
            focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"删除项目",focusProjectDO.toString()));
            return new BaReSult(true);
        }else{

            return new BaReSult("请联系管理员");
        }
    }

    @RequestMapping("/kpi")
    public String kpi(HttpServletRequest req,HttpServletResponse resp) throws IOException {

        String m = req.getParameter("m");
        String userid = req.getParameter("userid");
        System.out.println("userid:" + userid);
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        List<JiduVO> jiduList  = focusKpiService.getjifuList();
        req.setAttribute("jiduList",jiduList);

        List<FocusUserVO> userList = focusUserService.getFocusUserList();
        req.setAttribute("userList", userList);

        if(m.equals("list")){
            String jidu = StringUtil.getNullOrEmpty(req.getParameter("jidu"));
            List<FocusKpiVO>  kpiList  = null;
            if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
                if(StringUtil.isNullOrEmpty(userid)) {
                    kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, jidu);
                }else {
                    kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(userid, jidu);
                }
            }else{
                kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), jidu);
            }
            FocusKpiDO focusKpiDO = focusKpiService.getkpiListSum(kpiList);
            req.setAttribute("sum",focusKpiDO);
            req.setAttribute("downexcel", UserExportExcelUtil.kpiExcelexpot(kpiList, focusKpiDO,adminUser));
            req.setAttribute("kpiList",kpiList);
        }
        if(m.equals("add")){

            return ControllerUtil.toHtml(adminUser,"clazz/addkpi",req,resp);
        }
        if(m.equals("edit")){
            String id = req.getParameter("id");

            FocusKpiVO kpiEntity = focusKpiService .getFocusKpiDOById(id);

            req.setAttribute("kpiEntity",kpiEntity);
            return ControllerUtil.toHtml(adminUser,"clazz/addkpi",req,resp);
        }
        return "clazz/kpiList";
    }

    @ResponseBody
    @RequestMapping("/addkpi")
    public BaReSult addkpi(HttpServletRequest request,HttpServletResponse response,
                                  FocusKpiDO focusKpiDO) throws IOException {
        System.out.println("focusKpiDO"+focusKpiDO);
        Integer retInt = 0;
        FocusUserVO adminUser = ControllerUtil.checkuser(request,response);
        String m = request.getParameter("m");
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            if(m.equals("doadd")) {

                retInt = focusKpiService.addFocusKpiDO(focusKpiDO);
                if(retInt > 0){
                    focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"添加绩效",focusKpiDO.toString()));
                }
            } else {

                retInt = focusKpiService.updateFocusKpiDO(focusKpiDO);
                if(retInt > 0){
                    focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"修改绩效",focusKpiDO.toString()));
                }
            }
        }else {
            if (m.equals("doedit"))  {

                retInt = focusKpiService.updateFocusKpiDO(focusKpiDO);
                if(retInt > 0){
                    focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"修改绩效",focusKpiDO.toString()));
                }
            }
        }
        if(retInt>0){
            return new BaReSult(true);
        }else{
            return new BaReSult("请联系管理员");
        }

    }

    @ResponseBody
    @RequestMapping("/delkpi")
    public BaReSult delkpi(HttpServletResponse resp, HttpServletRequest req,
                           FocusKpiDO focusKpiDO
    ) throws IOException {
//        return new BaReSult("操作失败，请联系管理员");
        Integer retInt = 0;
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        if(adminUser.getPermission().getCode()== FocusdepaTypeEnum.BOSS.getCode()){

            retInt= focusKpiService.delFocusKpiDO(focusKpiDO);
        }else {
            return new BaReSult("权限不够");
        }
        if(retInt>0){
            focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"删除绩效",focusKpiDO.toString()));
            return new BaReSult(true);
        }else{
            return new BaReSult("请联系管理员");
        }
    }


    @ResponseBody
    @RequestMapping("/upload")
    public BaReSult upload(HttpServletResponse resp, HttpServletRequest req
                           ) throws IOException {
//        String fileName = myfile.getOriginalFilename(); //获取文件名
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
//        System.out.print("adminUser:"+adminUser.toString());
        //先创建本地文件夹
        String  img = new FileUploadUtil().saveFocusOneFile(req);
//        System.out.print("img:"+img);
        if(StringUtil.isNullOrEmpty(img)){
            return new BaReSult(false,"文件上传失败");
        }else{
            focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"上传报表",img));
            return new BaReSult(true,img);
        }
    }




    @RequestMapping("/unfinished")
    public String  unfinished(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String m = req.getParameter("m");
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        ControllerUtil.saveuser(req, focusProjectService, focusUserService, adminUser);
        List<FocusProduceDO>  produceList  = focusProduceService.getFocusProduceDOList();

        List<JiduVO> jiduList  = focusKpiService.getjifuList();
        req.setAttribute("jiduList",jiduList);

        List<FocusUserVO> aes  = focusUserService.getFocusUserListbypermission(FocusdepaTypeEnum.AE.getCode());
//        List<FocusCategoryTypeVO> focusCategoryTypeVOList = focusProjectService.getFocusCategoryTypeVOList();
        List<FocusStatusTypeVO> focusStatusTypeVOList = focusProjectService.getFocusStatusTypeVOList();

        req.setAttribute("produceList",produceList);
        req.setAttribute("aes",aes);
//        req.setAttribute("categorys",focusCategoryTypeVOList);
        req.setAttribute("status",focusStatusTypeVOList);

//        req.setAttribute("jiduList",jiduList);

        if(m.equals("list")){
            List<FocusProjectVO> projectList =null;
            String ptid = StringUtil.getNullOrEmpty(req.getParameter("ptid"));
            String jidu = StringUtil.getNullOrEmpty(req.getParameter("jidu"));
            String name = StringUtil.getNullOrEmpty(req.getParameter("key"));
            String aeid = StringUtil.getNullOrEmpty(req.getParameter("aesid"));
            String statuid = StringUtil.getNullOrEmpty(req.getParameter("statuid"));

            if(adminUser.getPermission().getCode() != FocusdepaTypeEnum.AE.getCode()){
                if(!StringUtil.isNullOrEmpty(jidu)) {
                    String[] timeStr= jidu.split("_");

                    projectList =   focusProjectService.getFocusUnfinishedProjectVOListByTimeandprid(null, timeStr, ptid, name, aeid, statuid, adminUser.getPermission().getCode());

                }else {
                    projectList =   focusProjectService.getFocusUnfinishedProjectVOListByTimeandprid(null, null, ptid, name, aeid, statuid, adminUser.getPermission().getCode());
                }
            }else{
                if(!StringUtil.isNullOrEmpty(jidu)) {
                    String[] timeStr= jidu.split("_");
                    projectList =   focusProjectService.getFocusUnfinishedProjectVOListByTimeandprid(adminUser.getId(), timeStr, ptid, name, aeid, statuid,adminUser.getPermission().getCode());
                }else {
                    projectList =   focusProjectService.getFocusUnfinishedProjectVOListByTimeandprid(adminUser.getId(), null, ptid, name, aeid, statuid,adminUser.getPermission().getCode());
                }
            }
            FocusKpiDO focusKpiDO = focusKpiService.getprojectListSum(projectList);
            req.setAttribute("sum",focusKpiDO);
            req.setAttribute("downexcel", UserExportExcelUtil.projectExcelexpot(projectList, focusKpiDO,adminUser));
            req.setAttribute("projectList",projectList);
        }
        if(m.equals("add")){

            return ControllerUtil.toHtml(adminUser,"clazz/addunfinished",req,resp);
        }
        if(m.equals("edit")){
            String id = req.getParameter("id");
            FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(id);
            req.setAttribute("projectEntity",projectEntity);
            return ControllerUtil.toHtml(adminUser,"clazz/addunfinished",req,resp);
        }
        if(m.equals("check")){
            String id = req.getParameter("id");
            FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(id);
            req.setAttribute("projectEntity",projectEntity);
            return ControllerUtil.toHtml(adminUser,"clazz/checkunfinished",req,resp);
        }
        return "clazz/unfinishedList";
    }


    @ResponseBody
    @RequestMapping("/addunfinished")
    public BaReSult addunfinished(HttpServletRequest req,HttpServletResponse resp,FocusProjectDO focusProjectDO ) throws IOException {
        String m = req.getParameter("m");
        System.out.println(focusProjectDO.toString());
        Integer retInt = 0;
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        NumberFormat format = NumberFormat.getNumberInstance() ;
        format.setMaximumFractionDigits(2);

        Double planrate  = (CommonUtil.stringToDouble(focusProjectDO.getPlannum())- CommonUtil.stringToDouble(focusProjectDO.getPlancomoutnum())- CommonUtil.stringToDouble(focusProjectDO.getPlanprioutnum()))/(1+CommonUtil.stringToDouble(focusProjectDO.getTaxrate()));
        focusProjectDO.setPlanrate(format.format(planrate));
        Double realrate  = (CommonUtil.stringToDouble(focusProjectDO.getPonum())- CommonUtil.stringToDouble(focusProjectDO.getRealcomoutnum())- CommonUtil.stringToDouble(focusProjectDO.getRealprioutnum()))/(1+CommonUtil.stringToDouble(focusProjectDO.getTaxrate()));
        focusProjectDO.setRealrate(format.format(realrate));


        retInt = focusProjectService.updateFocusProjectDObyId(focusProjectDO);
        if(retInt > 0){
            focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"更新未完项目",focusProjectDO.toString()));
        }
//        if(adminUser.getPermission().getCode() <= FocusdepaTypeEnum.AE.getCode()){
//            if(m.equals("doadd")) {
//                focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"添加项目",focusProjectDO.toString()));
//                retInt = focusProjectService.addFocusProjectDO(focusProjectDO);
//            } else {
//                focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"更新项目",focusProjectDO.toString()));
//                retInt = focusProjectService.updateFocusProjectDObyId(focusProjectDO);
//            }
//        }else {
//            if (m.equals("doedit"))  {
//                focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"更新项目",focusProjectDO.toString()));
//                retInt = focusProjectService.updateFocusProjectDObyId(focusProjectDO);
//            }
//        }

        if(retInt > 0){
            return new BaReSult(true);
        }else{
            return new BaReSult("请联系管理员");
        }
    }



    @RequestMapping("/hasfinished")
    public String  hasfinished(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String m = req.getParameter("m");
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        List<FocusProduceDO>  produceList  = focusProduceService.getFocusProduceDOList();

        List<JiduVO> jiduList  = focusKpiService.getjifuList();
        req.setAttribute("jiduList",jiduList);

        List<FocusUserVO> aes  = focusUserService.getFocusUserListbypermission(FocusdepaTypeEnum.AE.getCode());
//        List<FocusCategoryTypeVO> focusCategoryTypeVOList = focusProjectService.getFocusCategoryTypeVOList();
        List<FocusStatusTypeVO> focusStatusTypeVOList = focusProjectService.getFocusStatusTypeVOList();

        req.setAttribute("produceList",produceList);
        req.setAttribute("aes",aes);
//        req.setAttribute("categorys",focusCategoryTypeVOList);
        req.setAttribute("status",focusStatusTypeVOList);

//        req.setAttribute("jiduList",jiduList);

        if(m.equals("list")){
            List<FocusProjectVO> projectList =null;
            String ptid = StringUtil.getNullOrEmpty(req.getParameter("ptid"));
            String jidu = StringUtil.getNullOrEmpty(req.getParameter("jidu"));
            String name = StringUtil.getNullOrEmpty(req.getParameter("key"));
            String aeid = StringUtil.getNullOrEmpty(req.getParameter("aesid"));
            String statuid = StringUtil.getNullOrEmpty(req.getParameter("statuid"));

            if(adminUser.getPermission().getCode() != FocusdepaTypeEnum.AE.getCode()){
                if(!StringUtil.isNullOrEmpty(jidu)) {
                    String[] timeStr= jidu.split("_");

                    projectList =   focusProjectService.getFocushasFinishedProjectVOListByTimeandprid(null, timeStr, ptid, name, aeid, statuid);

                }else {
                    projectList =   focusProjectService.getFocushasFinishedProjectVOListByTimeandprid(null, null, ptid, name, aeid, statuid);
                }
            }else{
                if(!StringUtil.isNullOrEmpty(jidu)) {
                    String[] timeStr= jidu.split("_");
                    projectList =   focusProjectService.getFocushasFinishedProjectVOListByTimeandprid(adminUser.getId(), timeStr, ptid, name, aeid, statuid);
                }else {
                    projectList =   focusProjectService.getFocushasFinishedProjectVOListByTimeandprid(adminUser.getId(), null, ptid, name, aeid, statuid);
                }
            }
            FocusKpiDO focusKpiDO = focusKpiService.getprojectListSum(projectList);
            req.setAttribute("sum",focusKpiDO);
            req.setAttribute("downexcel", UserExportExcelUtil.projectExcelexpot(projectList, focusKpiDO,adminUser));
            req.setAttribute("projectList",projectList);
        }
        if(m.equals("add")){

            return ControllerUtil.toHtml(adminUser,"clazz/addhasfinished",req,resp);
        }
        if(m.equals("edit")){
            String id = req.getParameter("id");
            FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(id);
            req.setAttribute("projectEntity",projectEntity);
            return ControllerUtil.toHtml(adminUser,"clazz/addhasfinished",req,resp);
        }
        if(m.equals("check")){
            String id = req.getParameter("id");
            FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(id);
            req.setAttribute("projectEntity",projectEntity);
            return ControllerUtil.toHtml(adminUser,"clazz/checkhasfinished",req,resp);
        }
        return "clazz/hasfinishedList";
    }
    @ResponseBody
    @RequestMapping("/addfinished")
    public BaReSult addfinished(HttpServletRequest req,HttpServletResponse resp,FocusProjectDO focusProjectDO ) throws IOException {
        String m = req.getParameter("m");
        System.out.println(focusProjectDO.toString());
        Integer retInt = 0;
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        NumberFormat format = NumberFormat.getNumberInstance() ;
        format.setMaximumFractionDigits(2);

        Double planrate  = (CommonUtil.stringToDouble(focusProjectDO.getPlannum())- CommonUtil.stringToDouble(focusProjectDO.getPlancomoutnum())- CommonUtil.stringToDouble(focusProjectDO.getPlanprioutnum()))/(1+CommonUtil.stringToDouble(focusProjectDO.getTaxrate()));
        focusProjectDO.setPlanrate(format.format(planrate));
        Double realrate  = (CommonUtil.stringToDouble(focusProjectDO.getPonum())- CommonUtil.stringToDouble(focusProjectDO.getRealcomoutnum())- CommonUtil.stringToDouble(focusProjectDO.getRealprioutnum()))/(1+CommonUtil.stringToDouble(focusProjectDO.getTaxrate()));
        focusProjectDO.setRealrate(format.format(realrate));


        retInt = focusProjectService.updateFocusProjectDObyId(focusProjectDO);
        if(retInt > 0){
            focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"更新未完项目",focusProjectDO.toString()));
        }
        if(retInt > 0){
            return new BaReSult(true);
        }else{
            return new BaReSult("请联系管理员");
        }
    }
    @RequestMapping("/process")
    public String  process(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String m = req.getParameter("m");
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        List<FocusProduceDO>  produceList  = focusProduceService.getFocusProduceDOList();

        List<JiduVO> jiduList  = focusKpiService.getjifuList();
        req.setAttribute("jiduList",jiduList);

        List<FocusUserVO> aes  = focusUserService.getFocusUserListbypermission(FocusdepaTypeEnum.AE.getCode());
//        List<FocusCategoryTypeVO> focusCategoryTypeVOList = focusProjectService.getFocusCategoryTypeVOList();
        List<FocusStatusTypeVO> focusStatusTypeVOList = focusProjectService.getFocusStatusTypeVOList();

        req.setAttribute("produceList",produceList);
        req.setAttribute("aes",aes);
//        req.setAttribute("categorys",focusCategoryTypeVOList);
        req.setAttribute("status",focusStatusTypeVOList);

//        req.setAttribute("jiduList",jiduList);

        if(m.equals("list")){
            List<FocusProjectVO> projectList =null;
            String ptid = StringUtil.getNullOrEmpty(req.getParameter("ptid"));
            String jidu = StringUtil.getNullOrEmpty(req.getParameter("jidu"));
            String name = StringUtil.getNullOrEmpty(req.getParameter("key"));
            String aeid = StringUtil.getNullOrEmpty(req.getParameter("aesid"));
            String statuid = StringUtil.getNullOrEmpty(req.getParameter("statuid"));

            if(adminUser.getPermission().getCode() != FocusdepaTypeEnum.AE.getCode()){
                if(!StringUtil.isNullOrEmpty(jidu)) {
                    String[] timeStr= jidu.split("_");

                    projectList =   focusProjectService.getFocushasProcessProjectVOListByTimeandprid(null, timeStr, ptid, name, aeid, statuid);

                }else {
                    projectList =   focusProjectService.getFocushasProcessProjectVOListByTimeandprid(null, null, ptid, name, aeid, statuid);
                }
            }else{
                if(!StringUtil.isNullOrEmpty(jidu)) {
                    String[] timeStr= jidu.split("_");
                    projectList =   focusProjectService.getFocushasProcessProjectVOListByTimeandprid(adminUser.getId(), timeStr, ptid, name, aeid, statuid);
                }else {
                    projectList =   focusProjectService.getFocushasProcessProjectVOListByTimeandprid(adminUser.getId(), null, ptid, name, aeid, statuid);
                }
            }
            FocusKpiDO focusKpiDO = focusKpiService.getprojectListSum(projectList);
            req.setAttribute("sum",focusKpiDO);
            req.setAttribute("downexcel", UserExportExcelUtil.projectExcelexpot(projectList, focusKpiDO,adminUser));
            req.setAttribute("projectList",projectList);
        }
        if(m.equals("edit")){
            String id = req.getParameter("id");
            FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(id);
            req.setAttribute("projectEntity",projectEntity);
            return ControllerUtil.toHtml(adminUser,"clazz/addprocess",req,resp);
        }
        if(m.equals("check")){
            String id = req.getParameter("id");
            FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(id);
            req.setAttribute("projectEntity",projectEntity);
            return ControllerUtil.toHtml(adminUser,"clazz/checkprocess",req,resp);
        }
        return "clazz/processList";
    }

    @ResponseBody
    @RequestMapping("/addprocess")
    public BaReSult addprocess(HttpServletRequest req,HttpServletResponse resp,FocusProjectDO focusProjectDO ) throws IOException {
        String m = req.getParameter("m");
        System.out.println(focusProjectDO.toString());
        Integer retInt = 0;
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        NumberFormat format = NumberFormat.getNumberInstance() ;
        format.setMaximumFractionDigits(2);

        Double planrate  = (CommonUtil.stringToDouble(focusProjectDO.getPlannum())- CommonUtil.stringToDouble(focusProjectDO.getPlancomoutnum())- CommonUtil.stringToDouble(focusProjectDO.getPlanprioutnum()))/(1+CommonUtil.stringToDouble(focusProjectDO.getTaxrate()));
        focusProjectDO.setPlanrate(format.format(planrate));
        Double realrate  = (CommonUtil.stringToDouble(focusProjectDO.getPonum())- CommonUtil.stringToDouble(focusProjectDO.getRealcomoutnum())- CommonUtil.stringToDouble(focusProjectDO.getRealprioutnum()))/(1+CommonUtil.stringToDouble(focusProjectDO.getTaxrate()));
        focusProjectDO.setRealrate(format.format(realrate));


        retInt = focusProjectService.updateFocusProjectDObyId(focusProjectDO);
        if(retInt > 0){
            focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"更新未完项目",focusProjectDO.toString()));
        }
        if(retInt > 0){
            return new BaReSult(true);
        }else{
            return new BaReSult("请联系管理员");
        }
    }
}
